
var app = getApp()
Page({
  data: {
    // text:"这是一个页面"
    navList: [],
    goodsList: [],
    id: 0,
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    scrollHeight: 0,
    page: 1,
    size: 10000
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    if (options.second_id && options.third_id) {
      that.setData({
        id: parseInt(options.second_id),
        third_id: parseInt(options.third_id)
      });
    }

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });


    this.getCategoryInfo();

  },
  getCategoryInfo: function () {
    let that = this;
    wx.request({
      url: app.globalData.domain + '/item_cats/' + that.data.id+'/',
      header: {
        'Authorization': 'JWT ' + wx.getStorageSync('token')
      },
      success:function(res){
        that.setData({
          navList: res.data.child_list
        })
        that.getGoodsList();
      },
    })
    // util.request(api.GoodsCategory, { id: this.data.id })
    //   .then(function (res) {

    //     if (res.errno == 0) {
    //       that.setData({
    //         navList: res.data.brotherCategory,
    //         currentCategory: res.data.currentCategory
    //       });
    //nav位置
      // let currentIndex = 0;
      // let navListCount = that.data.navList.length;
      // for (let i = 0; i < navListCount; i++) {
      //   currentIndex += 1;
      //   if (that.data.navList[i].id == that.data.id) {
      //     break;
      //   }
      // }
      // if (currentIndex > navListCount / 2 && navListCount > 5) {
      //   that.setData({
      //     scrollLeft: currentIndex * 60
      //   });
      // }
          
    //       that.getGoodsList();

    //     } else {
    //       //显示错误信息
    //     }
        
    //   });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    console.log(1);
  },
  onHide: function () {
    // 页面隐藏
  },
  getGoodsList: function () {
    var that = this;
    wx.request({
      url: app.globalData.domain + '/item/',
      header: {
        'Authorization': 'JWT ' + wx.getStorageSync('token')
      },
      data:{
        item_cats: that.data.third_id
      },
      success:function(res){
        that.setData({
          goodsList:res.data['results'],
        })
      },

    // util.request(api.GoodsList, {categoryId: that.data.id,  page: that.data.page, size: that.data.size})
    //   .then(function (res) {
    //     that.setData({
    //       goodsList: res.data.goodsList,
    //     });
    });
  },
  onUnload: function () {
    // 页面关闭
  },
  switchCate: function (event) { 
    if (this.data.third_id == event.currentTarget.dataset.id) {
      return false;
    }
    var that = this;
    var clientX = event.detail.x;
    var currentTarget = event.currentTarget;
    if (clientX < 60) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft - 60
      });
    } else if (clientX > 330) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft
      });
    }
    this.setData({
      third_id: event.currentTarget.dataset.id
    });

    this.getGoodsList();
  }
})